function Midi()
{
	this.createFromData = function(data)
	{
		var trackChunks, currentChunk, curPos, i, l;
		if (data.substr(0, 4) != 'MThd')
			throw('Data doesn\'t contain a MIDI file');
		this.header = new function MidiHeader()
		{
			this.format = data.charCodeAt(4) * 256 + data.charCodeAt(5);
			this.trackNumber = data.charCodeAt(6) * 256 + data.charCodeAt(7);
			this.deltaTime = data.charCodeAt(8) * 256 + data.charCodeAt(9);
		};
		trackChunks = data.split('MTrk');
		this.tracks = [];
		for (i=1; i<trackChunks.length; i++)
		{
			l = trackChunks[i].charCodeAt(4) * Math.pow(256, 3) + trackChunks[i].charCodeAt(5) * Math.pow(256, 2) + trackChunks[i].charCodeAt(6) * Math.pow(256, 1) + trackChunks[i].charCodeAt(7);
			currentChunk = trackChunks[i].substr(8, l);
			this.tracks.push(new function MidiTrack(){
				this.events = [];
				for (var i=0; i<l; i++)
				{
					if (currentChunk.charCodeAt(i) == 255)
						this.events.push(new function MidiEvent(){
							this.channel = 15;
							this.status = currentChunk.charCodeAt(i+1);
							this.data1 = currentChunk.charCodeAt(i+2);
							this.data2 = currentChunk.substr(i+3, this.data1);
							i += 3+this.data1;
						});
					else
						this.events.push(new function MidiEvent(){
						});
				}
			});
		}
	};
	this.createNew = function()
	{
	};

	if (arguments.length == 1 && typeof arguments[0] == 'string')
		this.createFromData(arguments[0]);
	else if(arguments.length == 3)
		'';
	else
		throw('Invalid constructor');
}
